{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>{$title}</legend>
    </fieldset>
    <form class="layui-form layui-form-pane" lay-filter="form">
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">设备名称</label>
            <div class="layui-input-4">
                <input type="text" name="dev_name" lay-verify="required" placeholder="请输入设备名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">设备号</label>
            <div class="layui-input-4">
                <input type="text" name="dev_mac" lay-verify="required" placeholder="请输入设备号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">选择大棚</label>
            <div class="layui-input-4">
                <select name="house_id" lay-verify="required" lay-filter="hid">
                    <option value="">选择大棚</option>
                    {volist name='house' id='vo'}
                    <option value="{$vo.id}">{$vo.house_name}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">选择关联网关</label>
            <div class="layui-input-4">
                <select name="gateway_id" id="gateway_id" lay-verify="required" lay-filter="gateway_id">
                    <option value="">关联网关</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">选择设备控制器</label>
            <div class="layui-input-4">
                <select name="control_id" id="control_id">
                    <option value="">请选择设备控制器</option>
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux">
                同一个控制器可以控制多个设备
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="submit">{:lang('submit')}</button>
                <a onclick="navigate_back()" class="layui-btn layui-btn-primary">{:lang('back')}</a>
            </div>
        </div>
    </form>
</div>
{include file="common/foot"/}
<script>
    layui.use(['form', 'layer'], function () {
        var form = layui.form, layer = layui.layer, $ = layui.jquery;
        form.on('select(hid)', function (data) {
            $.ajax({
                url: '{:url("devAdd")}',
                type: 'post',
                data: {'house_id': data.value},
                success: function (res) {
                    var opt = '';
                    if (res.length > 0) {
                        opt = '<option value="-1">选择关联网关</option>';
                        for (var i = 0; i < res.length; i++) {
                            opt += '<option value="' + res[i]['id'] + '">' + res[i]['gateway_name'] + '</option>';
                        }
                    } else {
                        opt = '<option >暂无网关</option>';
                    }
                    $("#gateway_id").html(opt);
                    form.render('select');
                }
            });
        });
        form.on('select(gateway_id)', function (data) {
                $.ajax({
                    url: '{:url("devAdd")}',
                    type: 'post',
                    data: {'gateway_id': data.value},
                    success: function (res) {
                        var opt = '';
                        if (res.length > 0) {
                            opt = '<option value="-1">选择关联控制器</option>';
                            for (var i = 0; i < res.length; i++) {
                                opt += '<option value="' + res[i]['id'] + '">' + res[i]['control_id'] + '</option>';
                            }
                        } else {
                            opt = '<option >暂无控制器</option>';
                        }
                        $("#control_id").html(opt);
                        form.render('select');
                    }
                });
        });


        form.on('submit(submit)', function (data) {
            loading = layer.load(1, {shade: [0.1, '#fff']});
            $.post('{:url("devAdd")}', data.field, function (res) {
                layer.close(loading);
                if (res.code > 0) {
                    layer.msg(res.msg, {time: 1800, icon: 1}, function () {
//                        location.href = res.url;
                        window.parent.location.reload();
                    });
                } else {
                    layer.msg(res.msg, {time: 1800, icon: 2});
                }
            });
        });
    });
    function navigate_back() {
        //parent.location.href="index";
        parent.layer.closeAll();
    }
</script>